/*
 * Copyright 2007-2010 
 * This file is part of DP.
 *
 * DP is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * DP is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * DP; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package PD.util;


import PD.data.Row;
import java.util.Comparator;

/**
 * @author Taken from MZmine2
 * http://mzmine.sourceforge.net/
 * 
 * Compare peak list rows either by ID, average m/z or median area of peaks
 * 
 */
public class PeakListRowSorter implements Comparator<Row> {

        private SortingProperty property;
        private SortingDirection direction;

        public PeakListRowSorter(SortingProperty property,
                SortingDirection direction) {
                this.property = property;
                this.direction = direction;
        }

        public int compare(Row row1, Row row2) {

                Double row1Value = getValue(row1);
                Double row2Value = getValue(row2);

                if (direction == SortingDirection.Ascending) {
                        return row1Value.compareTo(row2Value);
                } else {
                        return row2Value.compareTo(row1Value);
                }

        }

        private double getValue(Row row) {
                switch (property) {                     
                        case Weight:
                                return row.getWeight();
                        case ID:
                                return row.getID();
                }

                // We should never get here, so throw exception
                throw (new IllegalStateException());
        }
}
